import { defineStore } from "pinia";
import { ref } from "vue";
import type { drDataInfo } from "@/apis/modules/account"

/**
 * @description  导出用户的store
 */
export const useUserStore = defineStore(
  "user",
  () => {
    /**@data */
    const userInfo = ref<userInfoType>({} as userInfoType);
    const drToken = ref<string>("");
    const drDetailInfo = ref<drDataInfo>({} as drDataInfo);
    
    /**@methods */
    /**@methods */
    const setToken = (token: string) => {
      drToken.value = token;
    };
    const clearToken = () => {
      drToken.value = "";
    };
    const saveUserInfo = (userInfos: userInfoType) => {
      console.debug(userInfos, "用户信息")
      userInfo.value = userInfos;
    };
    const clearUserInfo = () => {
      userInfo.value = {} as userInfoType;
    };
    // 详细信息
    const setDetailInfo = (detailInfo: any) => {
      drDetailInfo.value = detailInfo
    }
    const clearDrDetailInfo = () => {
      drDetailInfo.value = {} as drDataInfo
    }

    return {
      // 变量
      userInfo,
      drToken,
      drDetailInfo,
      // 方法
      setToken,
      clearToken,
      saveUserInfo,
      clearUserInfo,
      setDetailInfo,
      clearDrDetailInfo
    };
  },
  {
    // 配置持久化
    persist: {
      // 调整为兼容多端的API
      storage: {
        setItem(key, value) {
          uni.setStorageSync(key, value);
        },
        getItem(key) {
          return uni.getStorageSync(key);
        },
      },
    },
  }
);
/**
 * @description  用户信息类型ts
 */
class userInfoType {
  id: string;
  number: string;
  account: string;
  userTypeId: string;
  flag: number;
  openid: string | null;
  fkRoleId: string;
  token?: string;
  constructor(
    id: string,
    number: string,
    account: string,
    userTypeId: string,
    flag: number,
    openid: string | null,
    fkRoleId: string,
    token: string
  ) {
    this.account = account;
    this.id = id;
    this.number = number;
    this.userTypeId = userTypeId;
    this.flag = flag;
    this.openid = openid;
    this.fkRoleId = fkRoleId;
    this.token = token;
  }
}
